package com.xzh.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xzh.constant.RedisConstants;
import com.xzh.dao.UsersMapper;
import com.xzh.model.Users;
import com.xzh.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl implements UsersService {
    private UsersMapper usersMapper;
    private RedisTemplate redisTemplate;
    @Autowired
    public UsersServiceImpl(UsersMapper usersMapper) {
        this.usersMapper = usersMapper;
    }
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public Page<Users> selectAll(Page<Users> page, Users users) {
        return usersMapper.selectAll(page, users);
    }

    @Override
    public boolean insert(Users users) {
        boolean result = usersMapper.insert(users)>0;
        if (result && users.getAvatar() != null && !users.getAvatar().isEmpty()) {
            // 将图片URL添加到uploadImageToDB集合中
            redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, users.getAvatar());
        }
        return result;
    }

    @Override
    public boolean deleteById(Integer id) {
        return usersMapper.deleteById(id)>0;
    }

    @Override
    public boolean updateById(Users users) {
        boolean result = usersMapper.updateById(users)>0;
        if (result && users.getAvatar() != null && !users.getAvatar().isEmpty()) {
            // 将图片URL添加到uploadImageToDB集合中
            redisTemplate.opsForSet().add(RedisConstants.UPLOAD_IMAGE_TO_DB, users.getAvatar());
        }
        return result;
    }

    @Override
    public Users findByUsername(String username) {
        return usersMapper.findByUsername(username);
    }

    @Override
    public Users selectById(Integer id) {
        return usersMapper.selectById(id);
    }


}
